module.exports = class extends saas.controller {
  
  async _before() {
    const headers = this.request.headers;
    this.reply.header('Access-Control-Allow-Origin', headers.origin || "*");
    this.reply.header('Access-Control-Allow-Headers', "Origin, Token, StoreId, Content-Type, Accept, X-Requested-With");
    this.reply.header('Access-Control-Allow-Methods', "GET,POST,PUT,DELETE,OPTIONS");
    this.reply.header('Access-Control-Allow-Credentials', true);
    this.reply.header('Access-Control-Expose-Headers', 'app, token');
    const method = this.reply.request.raw.method; // 获取当前请求类型
    if (method === 'OPTIONS') {
      return this.success()
    }
    
    const token = headers.token;
    const loginService = this.service('public/loginService');
    const adminInfo = await loginService.getAdminInfoByToken(token);
    if (saas.isEmpty(adminInfo)) {
      return this.reply.send({code: 402,msg:'登录失效，请重新登录'})
    }
    this.admin = adminInfo;
    this.adminId = adminInfo.id;
    this.roleId = adminInfo.role_id;
  }
};
